package io.renren.modules.app.controller;

import java.awt.*;
import java.util.*;
import java.util.List;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-04-24 10:43:02
 */
@RestController
@RequestMapping("app/tcompany")
public class TCompanyController {
    @Autowired
    private TCompanyService tCompanyService;
    @Autowired
    private TProductService tProductService;
    @Autowired
    private TInsuranceService tInsuranceService;
    @Autowired
    private TAssureService tAssureService;
    @Autowired
    private TPolicyService tPolicyService;

    /**
     * 登录
     * @param account
     * @param pwd
     * @return
     */
    @RequestMapping("/login")
    public R login(String account, String pwd){
        TCompanyEntity  company = tCompanyService.login(account,pwd);
        Map<String, Object> map = new HashMap<>();
        if (company != null) {
            map.put("company", company);
            return R.ok(map);
        } else {
            return R.error("账号密码错误，请重新输入！");
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("app:tcompany:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tCompanyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("app:tcompany:info")
    public R info(@PathVariable("id") Long id){
        TCompanyEntity tCompany = tCompanyService.getById(id);

        return R.ok().put("tCompany", tCompany);
    }

    /**
     * 注册
     */
    @RequestMapping("/save")
    @RequiresPermissions("app:tcompany:save")
    public R save(@RequestBody TCompanyEntity tCompany){
        tCompanyService.save(tCompany);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("app:tcompany:update")
    public R update(@RequestBody TCompanyEntity tCompany){
        ValidatorUtils.validateEntity(tCompany);
        tCompanyService.updateById(tCompany);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("app:tcompany:delete")
    public R delete(@RequestBody Long[] ids){
        tCompanyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *公司信息注册
     * @param tCompany
     * @return
     */
    @RequestMapping("/saveCompanyInfo")
    public R saveCompanyInfo(TCompanyEntity tCompany){
        try {
            tCompany.setCreateTime(new Date());
            tCompanyService.save(tCompany);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     *获取贷款产品信息列表
     * @param
     * @return
     */
    @RequestMapping("/getProductInfo")
    public R getProductInfo(){
        List<TProductEntity> productList = new ArrayList<>();
        try {
            productList = tProductService.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("productList",productList);
    }

    /**
     *获取贷款某个产品详细信息
     * @param
     * @return
     */
    @RequestMapping("/getOneProductInfo")
    public R getOneProductInfo(String id){

        TProductEntity tProductEntity = new TProductEntity();
        try {
            tProductEntity = tProductService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("tProductEntity",tProductEntity);
    }

    /**
     *获取保险产品信息列表
     * @param
     * @return
     */
    @RequestMapping("/getSafeInfo")
    public R getSafeInfo(){
        List<TInsuranceEntity> insuranceList = new ArrayList<>();
        try {
            insuranceList = tInsuranceService.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("insuranceList",insuranceList);
    }


    /**
     *获取保险某个产品详细信息
     * @param
     * @return
     */
    @RequestMapping("/getOneSafeInfo")
    public R getOneSafeInfo(String id){

        TInsuranceEntity tInsuranceEntityt = new TInsuranceEntity();
        try {
            tInsuranceEntityt = tInsuranceService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("tInsuranceEntityt",tInsuranceEntityt);
    }

    /**
     *根据type获取所有产品信息列表
     * @param
     * @return
     */
    @RequestMapping("/getVouchOrFundInfo")
    public R getVouchInfo(String type){
        List<TAssureEntity> assureList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        try {
            assureList = (ArrayList)tAssureService.listByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("assureList",assureList);
    }

    /**
     * 获取政策指南
     * @return
     */
    @RequestMapping("/getPolicy")
    public R getPolicy(){
        List<TPolicyEntity> list = new ArrayList<>();
        try {
            list = tPolicyService.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("list",list);
    }

    /**
     * 获取政策指南详细
     * @return
     */
    @RequestMapping("/getOnePolicy")
    public R getOnePolicy(String id){
        TPolicyEntity tPolicyEntity = new TPolicyEntity();
        try {
            tPolicyEntity = tPolicyService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("tPolicyEntity",tPolicyEntity);
    }

    /**
     * 获取公司信息
     * @return
     */
    @RequestMapping("/getOneCompany")
    public R getOneCompany(String id){
        TCompanyEntity tCompanyEntity  = new TCompanyEntity();
        try {
            tCompanyEntity = tCompanyService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("tCompanyEntity",tCompanyEntity);
    }


    /**
     * 修改密码
     * @param id
     * @param password
     * @param newPwd1
     * @param newPwd2
     * @return
     */
    @RequestMapping("/changePassword")
    public R changePassword(String id,String password,String newPwd1,String newPwd2){
        TCompanyEntity tCompanyEntity = new TCompanyEntity();
        try {
            tCompanyEntity =tCompanyService.getById(id);
            if(password.equals("") || newPwd1.equals("") || newPwd2.equals("")){
                return R.error("密码不能为空！");
            }
            if(tCompanyEntity.getPassword().equals(password)){
                if(newPwd1.equals(newPwd2)){
                    TCompanyEntity tCompanyEntity2 = new TCompanyEntity();
                    tCompanyEntity2.setId(Long.parseLong(id));
                    tCompanyEntity2.setPassword(newPwd1);
                    tCompanyService.updateById(tCompanyEntity2);
                }else {
                    return R.error("新密码不一致！");
                }
            }else {
                return R.error("原密码错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }


}
